package crawler

import (
	"github.com/gin-gonic/gin"
	"learn-land-crawler/app/service"
	"net/http"
	"strconv"
)

func getSave600XuePage(c *gin.Context) {
	go service.Save600XuePage()
	c.String(http.StatusOK, "Save600XuePage")
}

func getSave600XudDetail(c *gin.Context) {
	login := c.Query("login")
	if login == "" {
		go service.Save600XudDetail(nil)
		c.String(http.StatusOK, "Save600XudDetail")
		return
	}
	cookies, err := service.Login()
	for err != nil {
		cookies, err = service.Login()
	}
	go service.Save600XudDetail(cookies)
	c.String(http.StatusOK, "Save600XudDetail by login")
}

func getSave600XudDownload(c *gin.Context) {
	l := 8
	limit := c.Query("limit")
	if limit != "" {
		ll, err := strconv.Atoi(limit)
		if err == nil {
			l = ll
		}
	}
	service.Save600XudDownload(l)
	c.String(http.StatusOK, "Save600XudDownload")
}

func convertToTProduct(c *gin.Context) {
	id := c.Query("id")
	if id != "" {
		iid, err := strconv.Atoi(id)
		if err == nil {
			go service.ConverToTProductBy(iid)
			c.String(http.StatusOK, "id convertToTProduct")
			return
		}
	}

	l := 0
	limit := c.Query("limit")
	if limit != "" {
		ll, err := strconv.Atoi(limit)
		if err == nil {
			l = ll
		}
	}
	go service.ConverToTProduct(l)
	c.String(http.StatusOK, "convertToTProduct")
}
